package com.yj.easynavigationbarlibaray;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Sigthp-P-Department on 2018/4/9.
 */

public class EasyNavigationBar {
    NavigationController.NavigationParams mParams;
    private EasyNavigationBar(NavigationController.NavigationParams params) {
        this.mParams=params;
        params.apply();
    }

    public void setText(int id, String text) {
        TextView  textView  = findViewById(id);
        if(!TextUtils.isEmpty(text)){
            textView.setVisibility(View.VISIBLE);
            textView.setText(text);
        }
    }

    public void setImageResource(int id, int resourceId) {
        ImageView imageView = findViewById(id);
        if(resourceId!=0){
            imageView.setVisibility(View.VISIBLE);
            imageView.setImageResource(resourceId);
        }
    }

    public void setAViewOnClickListener(int id, View.OnClickListener onClickListener) {
        findViewById(id).setOnClickListener(onClickListener);
    }

    public <T extends View> T findViewById(int id){
        if(mParams.mView!=null){
            return (T) mParams.mView.findViewById(id);
        }else{
            try {
                throw new IllegalAccessException("没有找到控件,请检查id");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    public <T extends View> T getAView(int id) {

        return findViewById(id);
    }


    public static class Builder{
        public Context mContext;
        private final NavigationController.NavigationParams params;

        public Builder(Context context){
            params = new NavigationController.NavigationParams(context);
            mContext=context.getApplicationContext();
        }

        public EasyNavigationBar builder(){
            EasyNavigationBar navigationBar = new EasyNavigationBar(params);
            return navigationBar;
        }

        public Builder setContentView(View view){
            params.mView = view;
            params.mViewLayoutResId = 0;
            return this;
        }

        public Builder setContentView(int layoutId){
            params.mView = null;
            params.mViewLayoutResId = layoutId;
            return this;
        }
    }
}
